package com.zer0.tga.entity.livingentity.mob;

import com.zer0.tga.art.ArtArchive;
import com.zer0.tga.art.Bitmap;
import com.zer0.tga.entity.Entity;
import com.zer0.tga.entity.abstractentity.Blinker;
import com.zer0.tga.entity.livingentity.Teemo;
import com.zer0.tga.entity.livingentity.flyingentity.TeemoDart;
import com.zer0.tga.level.Room;
import com.zer0.tga.math.Box;
import com.zer0.tga.math.Coordinate;
import com.zer0.tga.math.Direction;
import com.zer0.tga.math.RandNum;
import com.zer0.tga.tile.Tile;

public class GiantWolf extends Mob{
	
	
	private boolean clockwise = true;
	
	private int preparation=10;

	private Coordinate playerPos = null;
	
	private Direction hugWall = Direction.CENTER;
	private boolean huggingWall = false;
	
	private double movement_x;
	private double movement_y;
	
	
	public void init()
	{
		//System.out.println("init");
		unitType = UnitType.ENEMY;
		health = maxHealth = 12;
		movementSpeed = 4.0;
		damage = 1;
		
		clockwise = RandNum.getRandNum(0,2) == 0 ? true : false ;
	}

	@Override
	public void action() 
	{
		if(hugWall == Direction.CENTER)
		{
			Box walls = new Box(1.5*Tile.WIDTH , 1.5*Tile.HEIGHT,(double)((Room.ROOM_X)-1.5)*Tile.WIDTH ,(double)((Room.ROOM_Y)-1.5)*Tile.HEIGHT);

			double distance = position.x - walls.x0; 
			hugWall = Direction.WEST;
			if(distance > walls.x1 - position.x) { distance = walls.x1 - position.x; hugWall = Direction.EAST; }
			if(distance > position.y - walls.y0) { distance = position.y - walls.y0; hugWall = Direction.NORTH; }
			if(distance > walls.y1 - position.y) { distance = walls.y1 - position.y; hugWall = Direction.SOUTH; }

			preparation = RandNum.getRandNum(200,300);
		}

		if(preparation > 0)
		{
		direction = hugWall;

		if(hugWall == Direction.WEST)
			move(-movementSpeed,0);
		else if(hugWall == Direction.EAST)
			move(movementSpeed,0);
		else if(hugWall == Direction.NORTH)
			move(0,-movementSpeed);
		else if(hugWall == Direction.SOUTH)
			move(0,movementSpeed);
		}
		else
		{
			move(movement_x,movement_y);
			if(huggingWall==true)
			{
				clockwise = RandNum.getRandNum(0,2) == 0 ? true : false ;
				movementSpeed = 4.0;
				preparation = RandNum.getRandNum(200,300);
				this.slideOnCollision = false;
			}
		}
		
		if(huggingWall)
		{
			
			if(preparation == 20)
			{
				for(Entity e:level.getCurrentRoom().entities)
				{
					if (e instanceof Teemo)
					{
						playerPos = new Coordinate(e.position.x,e.position.y);
						break;
					}		    	
				}
				if(playerPos != null)
					level.addEntityToCurrentRoom(new Blinker(new Coordinate(playerPos.x,playerPos.y),new Coordinate(20,20),0xa00000ff,10,10,5));

			}
			else if(preparation == 1)
			{
				movement_x = playerPos.x - position.x;
				movement_y = playerPos.y - position.y;
				
				// don't remember what this called atm =\
				// Pythagoras 
				double pyth =  Math.sqrt(movement_x*movement_x + movement_y*movement_y);
				
				movementSpeed = 9.0;
				movement_x = movementSpeed*movement_x/pyth;
				movement_y = movementSpeed*movement_y/pyth;
				
				this.slideOnCollision = true;
				huggingWall = false;
				
			/*	int i = RandNum.getRandNum(1, 4);
				if(i==0) 	  hugWall = Direction.NORTH;
				else if(i==1) hugWall = Direction.SOUTH;
				else if(i==2) hugWall = Direction.EAST;
				else if(i==3) hugWall = Direction.WEST;
				huggingWall = false;
				preparation = 200;
				*/
						
			}
			
			preparation--;
			//movementSpeed -= 0.001;
		}
		//System.out.println("Trying to move in " + modifier + (goingInX?"X":"Y") + " with pos " + position.x +" " + position.y +" and player in " + playerPos.x + " " + playerPos.y);
	
	
		
	}

	@Override
	public Bitmap getArt() {
		// TODO Auto-generated method stub
		return ArtArchive.badguy3[0][direction.getDirection()-1];
	}
	

	public boolean collision(Entity entity, Direction dir) 
    {
	//	position.x += movementSpeed*knockback_x;
	//	position.y += movementSpeed*knockback_y;
	//	shouldTurn = true;
	//	System.out.println("asd " + entity.getClass().getSimpleName());
		
		if(entity instanceof TeemoDart)
		{
			hurt(((TeemoDart) entity).damage);
			entity.removed = true;
			return true;
		}
		else if (entity instanceof Teemo)
		{
			//System.out.println("Collision!");
			((Teemo) entity).hurt(damage);
			return true;
		}
		else if (entity == null)
		{
			huggingWall = true;

			if(clockwise)
			{
				if(hugWall == Direction.WEST) hugWall = Direction.NORTH;
				else if(hugWall == Direction.EAST) hugWall = Direction.SOUTH;
				else if(hugWall == Direction.NORTH) hugWall = Direction.EAST;
				else if(hugWall == Direction.SOUTH) hugWall = Direction.WEST;
			}
			else
			{
				if(hugWall == Direction.WEST) hugWall = Direction.SOUTH;
				else if(hugWall == Direction.EAST) hugWall = Direction.NORTH;
				else if(hugWall == Direction.NORTH) hugWall = Direction.WEST;
				else if(hugWall == Direction.SOUTH) hugWall = Direction.EAST;
			}
		//	}
		}
		
		return false;

    }



}
